const jwt = require('jsonwebtoken');
// 密钥
const secretKey = process.env.JWT_SECRET_KEY;
const jwtHelper = {
    // 生成token
    generateToken: (user) => {
      const payload = {
        id: user.id,
        username: user.username,
        email: user.email,
      };
      return jwt.sign(payload, secretKey, { expiresIn: 24 * 60 * 60 }); // 1小时后过期
    },
    // 验证token
    verifyToken: (token) => {
        try {
          return jwt.verify(token, secretKey);
        } catch (err) {
          return null;
        }
    }
};
module.exports = jwtHelper;